﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesGDB;

namespace ISchool
{
    //作者：代兴梅
    //描述：系统初始默认需要加载图像，基于图像（包括矢量图层）资源是否配置到位进行判断
    class Initialization
    {
        //默认矢量文件路径
        private static string GdbDefault = "";
        //默认卫星地图路径
        private  static string TiffDefault="";

        //默认系统文件夹路径
        private string basePath = System.AppDomain.CurrentDomain.BaseDirectory;
        //是否允许直接进入程序
        private bool isallow = true;
        //缺失类型
        private ShortType isShortType;

       //枚举缺失类型的情形
        public  enum ShortType
        {
           NULL,GDB_SHP, TIFF, GDB_SHPandTIFF
        }
        //默认GDB完整路径
        public static string dGdbPath
        {
            get { return GdbDefault; }
            set { GdbDefault = value; }
        }
        //默认Tiff完整路径
        public static string dTiffPath
        {
            get { return TiffDefault; }
            set { TiffDefault = value; }
        }
        //是否允许直接进入程序
        public bool IsAllow
        {
            get { return isallow; }
        }

        //对外缺失类型
        public ShortType NoType
        {
            get { return isShortType; }
        }

        //实例化
        public Initialization()
        {
            //系统预设Gdb文件库
            GdbDefault = basePath + "..\\..\\IData\\";
            if (!isGdbThere())
            {
                isallow = false;
                this.isShortType = ShortType.GDB_SHP;
                MessageBox.Show("矢量文件未就绪");
                
            }
            //系统预设卫星地图库
            TiffDefault = basePath + "..\\..\\IData\\Data_Tiff\\Data_CDUT.tif";
            if (!isTiffThere())
            {
                isallow = false;
                if (this.isShortType == ShortType.GDB_SHP)
                {
                    this.isShortType = ShortType.GDB_SHPandTIFF;
                    return;
                }
                this.isShortType = ShortType.TIFF;
                MessageBox.Show("卫星底图文件未就绪");
            }
            
        }

        //GDB文件是否就绪
        private bool isGdbThere()
        {
            try
            {
                //GDB文件地址
                string gdbPath = "地理数据库.gdb";
                dGdbPath = GdbDefault + gdbPath;
                List<IFeatureClass> pFeatureClasses = new List<IFeatureClass>();
                IWorkspaceFactory testmY = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactory();

                IWorkspace pWorkspace = testmY.OpenFromFile(dGdbPath, 0);
                //数据页集
                IEnumDataset pEnumDataset = pWorkspace.get_Datasets(esriDatasetType.esriDTAny);
                pEnumDataset.Reset();
                if (pEnumDataset != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
                return false;
            }
        }

        //卫星地图文件是否就绪
        private bool isTiffThere()
        {
            try
            {
                IRasterLayer mRasterLayer = new RasterLayerClass();
                mRasterLayer.CreateFromFilePath(TiffDefault);
                if (mRasterLayer != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return false;
            }
        }
    }
}
